package com.eqdd.yiqidian.ui;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Vibrator;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ScanZXingActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.service.LocationObservable;
import com.eqdd.yiqidian.utils.MyCodeUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ScanQRUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.concurrent.TimeUnit;

import cn.bingoogolapple.qrcode.core.QRCodeView;

public class ScanZXingActivity extends BaseActivity {

    private ScanZXingActivityCustom dataBinding;
    private boolean isLight = false;
    private String address;
    private ScanQRUtil scanQRUtil;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_scan_zxing);
        initTopTitleBar(View.VISIBLE, "二维码识别");
    }

    @Override
    public void initData() {
        scanQRUtil = new ScanQRUtil(ScanZXingActivity.this);
        RxPermissions.getInstance(this).request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(isGrant -> LocationObservable.get().subscribe(aMapLocation -> {
                    if (aMapLocation.getErrorCode() == 0) {
                        address = aMapLocation.getAddress();
                    }
                }));
        dataBinding.includeTitle.tvRight.setVisibility(View.VISIBLE);
        dataBinding.includeTitle.tvRight.setText("相册");
        dataBinding.includeTitle.tvRight.setOnClickListener((v) -> PicUtil.single(ScanZXingActivity.this)
        );

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    protected void onStart() {
        super.onStart();
        dataBinding.zxing.startCamera();
        dataBinding.zxing.showScanRect();
    }

    @Override
    protected void onDestroy() {
        dataBinding.zxing.onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        dataBinding.zxing.stopCamera();
        super.onStop();
    }

    @Override
    public void setView() {
        dataBinding.zxing.setDelegate(new QRCodeView.Delegate() {
            @Override
            public void onScanQRCodeSuccess(String result) {
                dataBinding.zxing.startSpot();
                vibrate();
                scanQRUtil.dealQR(result, address);
            }

            @Override
            public void onScanQRCodeOpenCameraError() {
                dataBinding.zxing.startSpot();

            }
        });
        dataBinding.zxing.startSpot();
        RxView.clicks(dataBinding.tvLight)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(v -> {
                    if (isLight) {
                        dataBinding.zxing.closeFlashlight();
                        dataBinding.tvLight.setText("打开手电筒");
                        isLight = false;
                    } else {
                        dataBinding.zxing.openFlashlight();
                        dataBinding.tvLight.setText("关闭手电筒");
                        isLight = true;
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    showLoading();
                    MyCodeUtil.analyzeBitmap(PictureSelector.obtainMultipleResult(data).get(0).getPath(), new MyCodeUtil.OnAnalyzeCallback() {
                        @Override
                        public void onAnalyzeSuccess(String content) {
                            hideLoading();
                            scanQRUtil.dealQR(content, address);
                        }
                        @Override
                        public void onAnalyzeFailed() {

                            hideLoading();
                            ToastUtil.showShort("二维码解析失败");
                        }
                    });
                    break;
            }
        }
    }

}
